home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2483 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: Sun, 21 Jan 1996 22:10:10 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9601212110.AA19771@dxmint.cern.ch>
  8. References: <Pine.OSF.3.91.960119114608.18779E-100000@io.UWinnipeg.ca> <4dpian$gij@oxy.rust.net> <9601201820.AA01752@dxmint.cern.ch> <4dtqpq$sl2@oxy.rust.net>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. ebennett@rust.net writes:
  14.  
  15. >Dan Pop <danpop@mail.cern.ch> wrote:
  16. >
  17. >>ebennett@rust.net writes:
  18. >
  19. >>>Given some number, there is a neat trick to generate a mask which will
  20. >>>have exactly one bit set in it.  The bit set will be the least
  21. >>>significant non-zero bit in the original number:
  22. >>>
  23. >>>    mask = ~number & number;
  24.               ^
  25. >>>
  26. >>>Now, if number contained a single non-zero bit (and is therefore a
  27. >>>power of 2),  mask will be equal to number.  Therefore:
  28. >>>
  29. >>>   if (number == (~number & number))
  30.                      ^
  31. >>>   {
  32. >>>        /* number is a power of 2 */
  33. >>>   }
  34. >>>
  35. >>>Try it, you'll like it!
  36. >
  37. >>I don't think so.  ~number & number will _always_ give 0.  -number & number
  38. >>will actually work, except for the case when number == 0.  That is,
  39. >>assuming a two's complement implementation.  On a one's complement or
  40. >>sign-magnitude implementation it won't work at all.
  41. >
  42. >>Yet another guy who makes a fool of himself because he's too lazy to
  43. >>test his solution before posting.
  44. >
  45. >You are correct in that I made a typo in my message.  ~ should have
  46. >been -. 
  47.  
  48. It was NOT a typo, because you wrote ~ instead of - consistently, not in a
  49. single place.
  50.  
  51. >Where would you run into a one's complement or sign-magnitude
  52. >implementation?
  53.  
  54. These implementations are allowed by the C standard and there are people
  55. who actually ran into them.  When posting a solution to c.l.c you have to
  56. either consider all possible situations or document its limitations.
  57. Otherwise, someone else will do it :-)
  58.  
  59. >However, I do not feel that I made a fool of myself. Most of us are
  60. >not perfect as you apparently are. The rest of us make mistakes, and
  61. >learn from them. That does not make us fools.
  62.  
  63. Making mistakes doesn't make one a fool.  Posting mistakes which could have
  64. been easily detected by actually testing the proposed solution does make
  65. one a fool.  The difference between a smart guy and fool guy making the
  66. same mistake is that the smart one can discover it himself.
  67.  
  68. Nobody is perfect, but checking your advice with your compiler and your
  69. ANSI C reference book before posting will make you look almost perfect :-)
  70.  
  71. Dan
  72. -- 
  73. Dan Pop
  74. CERN, CN Division
  75. Email: danpop@mail.cern.ch 
  76. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  77.